﻿using System;
using Csla;
using Csla.Serialization;
using System.Data.Linq;
using BizControl.DalLinq;
using BizControl.Resources;

namespace BizControl.Library.BusinessObjects.People
{
  [Serializable()]
  public class PersonInfo : ReadOnlyBase<PersonInfo>, ISupportEmptyValue
  {
    public Guid Id { get; private set; }
    public string FirstName { get; private set; }
    public string LastName { get; private set; }
    public Binary Photo { get; private set; }
    public string FullName { get { return FirstName + " " + LastName; } }

    public override string ToString()
    {
      return FirstName;
    }

    private PersonInfo()
    { /* require use of factory methods */ }

    internal PersonInfo(Guid id, string firstName, string lastName, Binary photo)
    {
      Id = id;
      FirstName = firstName;
      LastName = lastName;
      Photo = photo;
    }

    internal PersonInfo(PersonBO user)
    {
      if (user != null)
      {
        Id = user.PersonId;
        FirstName = user.FirstName;
        LastName = user.LastName;
        Photo = user.Photo;
      }
      else
      {
        Id = Guid.Empty;
        FirstName = PropertiesNames.EmptyName;
        LastName = PropertiesNames.EmptyName;
      }
    }

    internal PersonInfo(Person user)
    {
      if (user != null)
      {
        Id = user.PersonId;
        FirstName = user.FirstName;
        LastName = user.LastName;
        Photo = user.Photo;
      }
      else
      {
        Id = Guid.Empty;
        FirstName = PropertiesNames.EmptyName;
        LastName = PropertiesNames.EmptyName;
      }
    }

    public PersonInfo(PersonInfo personInfo)
    {
      if (personInfo != null)
      {
        Id = personInfo.Id;
        FirstName = personInfo.FirstName;
        LastName = personInfo.LastName;
        Photo = personInfo.Photo;
      }
      else
      {
        Id = Guid.Empty;
        FirstName = PropertiesNames.EmptyName;
        LastName = PropertiesNames.EmptyName;
      }
    }

    public override bool Equals(object obj)
    {
      if (obj is PersonInfo)
        return this.Id == (obj as PersonInfo).Id;
      return base.Equals(obj);
    }

    public override int GetHashCode()
    {
      return base.GetHashCode();
    }

    #region ISupportEmtpyValue Members

    public bool IsEmpty()
    {
      return Id == Guid.Empty;
    }

    #endregion
  }
}
